/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.model;

import android.content.Context;
import android.content.SharedPreferences;

import zuo.biao.library.base.BaseModel;

public class SensorSetInfo extends BaseModel {

    private static final long serialVersionUID = 1L;

    //地图设置
    private static String SENSORSETINFO = "sensorset";

    private static String SENSORSET_WENDU = "sensorset_wendux";//自定义温度
    private static String SENSORSET_HEIGHTQIYA = "sensorset_heightqiyad";//自定义海平面气压
    private static String SENSORSET_HEIGHTUSE = "sensorset_heightuse";//自定义海平面气压
    private static String SENSORSET_AUTOJZ = "sensorset_autojz";//自动使用GPS高度进行校准
    private static String SENSORSET_QIYAMODE = "sensorset_qiyamode";//气压校准模式 0，1，2

    private double height_qiya = 1013.25;//毫巴 -- 自定义海平面气压
    private float wenduX = 0;//度 温度纠偏值
    private boolean heightuse = true;
    private boolean gpsautojz = false;
    private int qiyamode = 0;

    private static SensorSetInfo instance = new SensorSetInfo();

    public static SensorSetInfo getInstance() {
        return instance;
    }

    /**
     * 默认构造方法，JSON等解析时必须要有
     */
    public SensorSetInfo() {
        //default
    }

    public void save(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                SENSORSETINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(SENSORSET_HEIGHTQIYA, this.height_qiya + "");
        editor.putFloat(SENSORSET_WENDU, this.wenduX);
        editor.putBoolean(SENSORSET_HEIGHTUSE, this.heightuse);
        editor.putBoolean(SENSORSET_AUTOJZ, this.gpsautojz);
        editor.putInt(SENSORSET_QIYAMODE, this.qiyamode);
        editor.commit();
    }

    public void load(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                SENSORSETINFO, Context.MODE_PRIVATE);
        this.height_qiya = Double.valueOf(sp.getString(SENSORSET_HEIGHTQIYA, this.height_qiya + ""));
        this.wenduX = sp.getFloat(SENSORSET_WENDU, this.wenduX);
        this.heightuse = sp.getBoolean(SENSORSET_HEIGHTUSE, this.heightuse);
        this.gpsautojz = sp.getBoolean(SENSORSET_AUTOJZ, this.gpsautojz);
        this.qiyamode = sp.getInt(SENSORSET_QIYAMODE, this.qiyamode);
    }

    public void reset() {
        height_qiya = 1013.25;
        wenduX = 35;
        gpsautojz = false;
        heightuse = true;
        qiyamode = 0;
    }

    public int getQiyamode() {
        return qiyamode;
    }

    public void setQiyamode(int qiyamode) {
        this.qiyamode = qiyamode;
    }

    public double getHeight_qiya() {
        return height_qiya;
    }

    public void setHeight_qiya(double height_qiya) {
        this.height_qiya = height_qiya;
    }

    public float getWenduX() {
        return wenduX;
    }

    public void setWenduX(float wenduX) {
        this.wenduX = wenduX;
    }

    public boolean isHeightuse() {
        return heightuse;
    }

    public void setHeightuse(boolean heightuse) {
        this.heightuse = heightuse;
    }

    public boolean isGpsautojz() {
        return gpsautojz;
    }

    public void setGpsautojz(boolean gpsautojz) {
        this.gpsautojz = gpsautojz;
    }

    @Override
    protected boolean isCorrect() {//根据自己的需求决定，也可以直接 return true
        return id > 0;// && StringUtil.isNotEmpty(phone, true);
    }

    //h -- 当前GPS高度  sPV--当前气压值（毫巴）
    public void useHeightCalibration(double h, double sPV) {
        double hg = 1013.25f;//hpa-毫巴

        double x1 = sPV / hg;
        double y1 = 1.0 / 5255.0;
        double altitude = 44330000.0 * (1 - (Math.pow(x1, y1)));

        double x = 1 + (h - altitude) / 44330000.0;
        double sPV2 = Math.pow(x, 5255.0) * hg;

//        double x2 = sPV / sPV2;
//        double y2 = 1.0 / 5255.0;
//        double altitude2 = 44330000.0 * (1 - (Math.pow(x2, y2)));

        setHeight_qiya(sPV2);
    }


}
